package com.itany.nmms.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.nmms.constant.DictConstant;
import com.itany.nmms.constant.StatusConstant;
import com.itany.nmms.dao.StaffMapper;
import com.itany.nmms.entity.Staff;
import com.itany.nmms.entity.StaffExample;
import com.itany.nmms.exception.CodeErrorException;
import com.itany.nmms.exception.MD5ErrorException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.StaffNotExistException;
import com.itany.nmms.service.StaffService;
import com.itany.nmms.util.MD5Util;
import com.itany.nmms.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 黄衡熙
 * @version 1.0
 */
@Service
@Transactional(propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffMapper staffMapper;
//    只读事务
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public Staff login(String loginName, String password, String role, String code, String image) throws RequestParameterErrorException, CodeErrorException, StaffNotExistException, MD5ErrorException {
        if(ParameterUtil.isNull(loginName) || ParameterUtil.isNull(password) || ParameterUtil.isNull(role) || ParameterUtil.isNull(code) || ParameterUtil.isNull(image)){
            throw new RequestParameterErrorException("请求参数有误");
        }

//        判断验证码
        if(!code.equals(image)){
            throw new CodeErrorException("验证码不正确");
        }
        // 登录查询,要求进行账号、密码、角色、是否在职的判断
        StaffExample example=new StaffExample();
        example.or()
                .andLoginNameEqualTo(loginName)
                .andPasswordEqualTo(MD5Util.md5(password))
                .andRoleEqualTo(role)
                .andStatusEqualTo(StatusConstant.STAFF_STATUS_ENABLED); // 必须在职
        List<Staff> staffs = staffMapper.selectByExample(example);
        if(staffs.isEmpty()){
            throw new StaffNotExistException("账号或密码错误");
        }
        return staffs.get(0);
    }
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public PageInfo<Staff> findByParams(Staff staffParam, String pageNo, String pageSize) {
//        第一步参数校验
        if(ParameterUtil.isNull(pageNo)){
            pageNo= DictConstant.STAFF_DEFAULT_PAGE_NO;
        }
        if(ParameterUtil.isNull(pageSize)){
            pageSize=DictConstant.STAFF_DEFAULT_PAGE_SIZE;
        }
//        分页三部取，计算总记录数，分页参数，分页查询
//        启动分页功能
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
//        执行查询操作
        List<Staff> staff=staffMapper.selectByParams(staffParam);
//        分页对象
        PageInfo<Staff> staffPage=new PageInfo<>(staff);
        return staffPage;
    }


}
